import { OrderRow } from './model';

export const getColor = (type: string) => {
  switch (type) {
    case '卸货':
      return 'rgba(76, 175, 80, 0.8)'; // 绿色
    case '发货':
      return 'rgba(255, 145, 31, 0.8)'; // 橙色
    case '上料':
      return 'rgba(246, 211, 101, 0.8)'; // 黄色
    case '收料':
      return 'rgba(158, 251, 211, 0.8)'; // 浅绿色
    case '倒垛':
      return 'rgba(255, 56, 107, 0.8)'; // 粉红色
    case '退料':
      return 'rgba(151, 151, 151, 0.8)'; // 灰色
    default:
      return 'rgba(255, 255, 255, 0.8)'; // 白色
  }
};

export const getLabelLeft = () => {
  const labelOffset = 30;
  return Math.cos(Math.PI / 4) * labelOffset;
};

export const getLabelTop = () => {
  return '-32px';
};

export const getLabelLeft1 = (pos: number) => {
  return `${20 * pos}px`;
};

export const getLabelTop1 = (pos: number) => {
  return `-${32 * pos}px`;
};

export const getLabelPos = (point: OrderRow, x: number, y: number, posMap: Record<string, any>) => {
  const labelWidth = 145;
  const labelHeight = 50;
  // const increaseArr = [
  //   [0, -1],
  //   [1, -1],
  //   [1, 0],
  //   [1, 1],
  //   [0, 1],
  //   [-1, -1],
  //   [-1, 0],
  //   [-1, -1],
  // ];
  const increaseArr = [
    [1, -1],
    [0, 1],
    [0, 1],
    [-1, 0],
    [-1, 0],
    [0, -1],
    [0, -1],
    [1, 0],
  ];

  // //获得px,py
  // export const getScaleX = (orderData: OrderRow[]) => {
  //     // if (orderData.length > 0) {
  //     //   const maxX = Math.max(...orderData.map(order => order.frX));
  //     //   const minX = Math.min(...orderData.map(order => order.frX));
  //     //   const maxX1 = Math.max(...orderData.map(order => order.toX));
  //     //   const minX1 = Math.min(...orderData.map(order => order.toX));
  //     //   const maxVal = Math.max(Math.abs(maxX), Math.abs(minX), Math.abs(maxX1), Math.abs(minX1));
  //     //   const rangeX = maxVal === 0 ? 10000 : maxVal * 2.6;
  //     //   return 1200 / rangeX;
  //     // } else {
  //     //   return 0;
  //     // }
  //     const maxVal = 160000 * 2.6;
  //     return 1000 / maxVal;
  //   };

  //   export const getScaleY = (orderData: OrderRow[]) => {
  //     // if (orderData.length > 0) {
  //     //   const maxY = Math.max(...orderData.map(order => order.frY));
  //     //   const minY = Math.min(...orderData.map(order => order.frY));
  //     //   const maxY1 = Math.max(...orderData.map(order => order.toY));
  //     //   const minY1 = Math.min(...orderData.map(order => order.toY));
  //     //   const maxVal = Math.max(Math.abs(maxY), Math.abs(minY), Math.abs(maxY1), Math.abs(minY1));
  //     //   const rangeY = maxVal === 0 ? 10000 : maxVal * 2.4;
  //     //   return 600 / rangeY;
  //     // } else {
  //     //   return 0;
  //     // }
  //     const maxVal = 21000 * 2.4;
  //     return 600 / maxVal;
  //   };

  const gridX = Math.floor(x / labelWidth);
  const gridY = Math.floor(y / labelHeight);
  posMap[`${gridX}-${gridY}`] = point.orderNo;
  const currCell = [gridX, gridY];
  let currPos = [];

  let found = false;
  // 如果格子已被占，循环查找
  if (posMap[`${currCell[0]}-${currCell[1]}`]) {
    while (!found) {
      console.log(1111);
      for (let i = 0; i < increaseArr.length; i++) {
        currCell[0] = currCell[0] + increaseArr[i][0];
        currCell[1] = currCell[1] + increaseArr[i][1];
        if (!posMap[`${currCell[0]}-${currCell[1]}`]) {
          found = true;
          posMap[`${currCell[0]}-${currCell[1]}`] = point.orderNo;
          currPos = [currCell[0] * labelWidth, currCell[1] * labelHeight];
          break;
        }
      }
      if (found) {
        break;
      }
    }
  } else {
    // 如果格子没有被占，就它了
    posMap[`${gridX}-${gridY}`] = point.orderNo;
  }
  currPos = [currCell[0] * labelWidth, currCell[1] * labelHeight];
  // const labelX = currCell[0] * labelWidth;
  // const labelY = currCell[1] * labelHeight;

  return { labelX: currPos[0], labelY: currPos[1] };
};

//
export const parseOrderData = (rows: any[]) => {
  const newRows = rows.sort((a, b) => a.frX - b.frX);
  let mark = 0;
  let mark2 = 0;
  const tt = newRows.map((v, idx) => {
    const next = newRows[idx + 1];
    if (next) {
      if (Math.abs(Math.abs(next.frX) - Math.abs(v.frX)) < 4000) {
        mark++;
      } else {
        mark = 1;
      }
    } else {
      mark = 1;
    }

    if (next) {
      if (mark === 1) {
        mark2 = 1;
      } else {
        if (Math.abs(Math.abs(next.frY) - Math.abs(v.frY)) < 4000) {
          mark2++;
        } else {
          mark2 = 1;
        }
      }
    } else {
      mark2 = 1;
    }

    return {
      pos1: mark,
      pos2: mark2,
      ...v,
    };
  });
  console.log(tt);
  return tt;
};

//获得px,py
export const getScaleX = (orderData: OrderRow[]) => {
  // if (orderData.length > 0) {
  //   const maxX = Math.max(...orderData.map(order => order.frX));
  //   const minX = Math.min(...orderData.map(order => order.frX));
  //   const maxX1 = Math.max(...orderData.map(order => order.toX));
  //   const minX1 = Math.min(...orderData.map(order => order.toX));
  //   const maxVal = Math.max(Math.abs(maxX), Math.abs(minX), Math.abs(maxX1), Math.abs(minX1));
  //   const rangeX = maxVal === 0 ? 10000 : maxVal * 2.6;
  //   return 1200 / rangeX;
  // } else {
  //   return 0;
  // }

  return (1200 / 160000) * 1.2;
};

export const getScaleY = (orderData: OrderRow[]) => {
  // if (orderData.length > 0) {
  //   const maxY = Math.max(...orderData.map(order => order.frY));
  //   const minY = Math.min(...orderData.map(order => order.frY));
  //   const maxY1 = Math.max(...orderData.map(order => order.toY));
  //   const minY1 = Math.min(...orderData.map(order => order.toY));
  //   const maxVal = Math.max(Math.abs(maxY), Math.abs(minY), Math.abs(maxY1), Math.abs(minY1));
  //   const rangeY = maxVal === 0 ? 10000 : maxVal * 2.4;
  //   return 600 / rangeY;
  // } else {
  //   return 0;
  // }
  return (600 / 21000) * 1.2;
};

export const parseOrderData1 = (
  rows: any[],
  scaleX: number,
  scaleY: number,
  obj: Record<string, any>,
  width: number,
  height: number,
) => {
  return rows.map(v => {
    const x = v.frX * scaleX + width / 2;
    const y = height / 2 - v.frY * scaleY;
    const x1 = v.toX * scaleX + width / 2;
    const y1 = height / 2 - v.toY * scaleY;
    const { labelX, labelY } = getLabelPos(v, x, y, obj);

    return {
      ...v,
      x,
      y,
      labelX,
      labelY,
      x1,
      y1,
    };
  });
};
